$m(function(args){	
	var dataSourceCode = args.dataSourceCode;
	var selectCallBack = args.callback;
	var $mainDialog = this.element;
	var $tree = $('#tempo-import-mapping-api-table-tree');
	
	var init = function(){
		$mainDialog.dialog({
			title:TempoUtils.locale("resource.select_table"),
			width:300,
			height:500,
			modal:true,
			resizable:false,
			close:function(){
				$mainDialog.dialog("destroy").remove();
			},
			open:function(){
				renderTableList();
			},
			buttons:[{
				type: 'ok',
				click:function(){
					var getSelected = $tree.tree('getChecked');
					if(!getSelected || getSelected.length <=0){
						$("<div/>").alert({
							msg:TempoUtils.locale("resource.please_select_dataset")
						});
						return; 
					}
					if(selectCallBack){
						selectCallBack.call(selectCallBack,getSelected[0]);
					}
					$mainDialog.dialog("close"); 
				}
			},{
				type : 'cancel',
				click:function(){
					$mainDialog.dialog("close"); 
				}
			}]
		});
	}
	
	
	
	//渲染数据列表
	var renderTableList = function(){
		var loading = $("<div/>").tempoprogressbar();
		$.ajax({
			url:contextPath + '/addTable/getAPITables',
			type:'post',
			success:function(resultData){
				loading.tempoprogressbar("destroy");
				if(resultData.flag){
					var tables = resultData.data;
					if(tables && tables.length>0){
						var tableMapping = {};
						for(var i=0;i<tables.length;i++){
							tableMapping[tables[i].name.toUpperCase()] = tables[i];
						}
						$tree.tree({
							url:contextPath + '/addTable/getAPITypes',
							idField:'id',
							textField:'title',
							checkbox:true,
							multiSelect:false,
							onBeforeExpand:function(nodeData,nodeParam){
								nodeParam['type'] = nodeData.type;
							},
							onBeforeRender:function(typeData){
								var datas = [];
								for(var j=0;j<typeData.length;j++){
									if(typeData[j].type == "table"){
										var target = tableMapping[typeData[j].id.toUpperCase()];
										if(target){
											target.icon = typeData[j].icon;
											datas.push(target);
										}
									}else{
										datas.push(typeData[j]);
									}
									if(typeData[j].id == '-1'){
										typeData[j].icon = "ui-icon tempo-ui-icon tempo-ui-icon-no-mutual tempo-ui-icon-api-folder-close";
									}
								}
								return datas;
							},
							isHiddenCheck:function(nodeData){
								if(nodeData.type == "TABLE"){
									return false;
								}
								return true;
							},
							onClickItem:function(){
								return false;
							}
						});
					}
				}else{
					$("<div/>").alert({
						msg:TempoUtils.locale("bi.datasource_gettable_fail_tip")
					});
				}
			}
		});
	}
	
	init();
});